package com.spgf.model.dao;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.dao.util.CustomSQLiteOpenHelper;
import com.spgf.model.entities.CategoriaModel;
import com.spgf.model.entities.IdentificacaoCategoriaModel;
import com.spgf.model.entities.LancamentoModel;
import com.spgf.model.entities.utils.ModelAb;
import com.spgf.model.utils.AndroidUtil;

@SuppressLint("SimpleDateFormat")
public class ExportarData {

	public void exportar(Context context) throws NegocioException {

		SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd", AndroidUtil.getLocalePadrao());

		List<ModelAb> list;

		StringBuilder s = new StringBuilder();
		s.append("CREATE TABLE [TBCATEGORIA] ([CD_CATEGORIA] INTEGER PRIMARY KEY,[ID_COR] NUMERIC,[NM_SIGLA] TEXT,[CD_SUPER_CATEGORIA] NUMERIC, [NM_CATEGORIA] TEXT,[TP_CATEGORIA] NUMERIC);");

		list = new CategoriaDAO(context).obterTodos();
		for (ModelAb m : list) {
			CategoriaModel l = (CategoriaModel) m;
			s.append("INSERT INTO TBCATEGORIA VALUES(" + l.getCodigo() + "," + l.getCor() + ",'" + l.getSigla() + "'," + l.getCategoriaSuperior().getCodigo() + ",'" + l.getNome() + "',"
					+ l.getTipo().getCodigo() + ");");
		}

		s.append("CREATE TABLE [TBIDENTIFICACAO_CATEGORIA] ([CD_IDENTIFICACAO_CATEGORIA] INTEGER PRIMARY KEY, [DS_CATEGORIA] VARCHAR NOT NULL, [CD_CATEGORIA] INTEGER NOT NULL CONSTRAINT [FK_IDENTIFICACAO_CAT_CATEGORIA] REFERENCES [TBCATEGORIA]([CD_CATEGORIA]) ON DELETE RESTRICT ON UPDATE RESTRICT);");

		list.clear();
		list = new IdentificacaoCategoriaDAO(context).obterTodos();
		for (ModelAb m : list) {
			IdentificacaoCategoriaModel l = (IdentificacaoCategoriaModel) m;
			s.append("INSERT INTO TBIDENTIFICACAO_CATEGORIA VALUES(" + l.getCodigo() + ", '" + l.getDescricaoCategoria() + "'," + l.getCategoria().getCodigo() + ");");
		}

		s.append("CREATE TABLE [TBLANCAMENTO] (  [CD_LANCAMENTO] INTEGER PRIMARY KEY,   [DT_LANCAMENTO] DATE NOT NULL,   [VL_LANCAMENTO] NUMERIC NOT NULL,   [DS_OBSERVACAO] TEXT,   [TP_FORMA_PAGAMENTO] NUMERIC NOT NULL,   [QT_PARCELAS] INTEGER NOT NULL DEFAULT 1,   [ID_SITUACAO] INTEGER(1) NOT NULL DEFAULT 1,   [CD_CATEGORIA] NUMERIC NOT NULL REFERENCES [TBCATEGORIA]([CD_CATEGORIA]) ON DELETE RESTRICT ON UPDATE RESTRICT);");

		list.clear();
		list = new LancamentoDAO(context).obterTodos();
		for (ModelAb m : list) {
			LancamentoModel l = (LancamentoModel) m;
			s.append("INSERT INTO TBLANCAMENTO VALUES(" + l.getCodigo() + ",'" + sdt.format(l.getData()) + "'," + l.getValor() + ",'" + l.getObservacao() + "'," + l.getFormaPagamento().getCodigo()
					+ "," + l.getNumeroParcelas() + "," + l.getSituacao().getCodigo() + "," + l.getCategoria().getCodigo() + ");");
		}

		 s.append("CREATE TABLE TBORCAMENTO (VL_ORCAMENTO NUMERIC, DT_FIM DATE, CD_ORCAMENTO INTEGER PRIMARY KEY, CD_CATEGORIA NUMERIC, DT_INICIO DATE);");

		 s.append("CREATE TABLE [TBPARAMETRO] ([CD_PARAMETRO] INTEGER  NOT NULL PRIMARY KEY,[ID_PARAMETRO] VARCHAR(10)  NOT NULL,[DS_PARAMETRO] VARCHAR(50)  NOT NULL,[VL_PARAMETRO] VARCHAR(50)  NOT NULL);");

		// CustomSQLiteOpenHelper.DB_PATH_EXTERNO;

		String[] linhas = s.toString().split(";");

		try {

			BufferedWriter buffWrite = new BufferedWriter(new FileWriter(CustomSQLiteOpenHelper.DB_PATH_EXTERNO + "exportacao.sql"));
			for (String string : linhas) {
				buffWrite.append(string+";" + "\n");
			}
			buffWrite.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
